/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        ListNode* i = nullptr;
        ListNode* j = head;
        ListNode* next;
        while (j) {
            next = j->next;
            j->next = i;
            i = j;
            j = next;
        }
        vector<int>ans;
        while (i) {
            ans.push_back(i->val);
            i = i->next;
        }
        return ans;
    }
};
